package com.wut.clock.controller;

import com.wut.clock.common.api.CommonResult;
import com.wut.clock.service.IHomeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author ZYH
 * @unit D9Lab
 * @description 首页管理 统计数据，统计UV(Unique Visitor),DAU(Daily Active User)
 * @date 2022/6/2 9:28
 */
@Api(value = "首页管理",tags = {"首页管理"})
@RestController
@RequestMapping("/home")
@RequiredArgsConstructor
public class HomeController {

    private final IHomeService homeService;

    @ApiOperation("记录UV(Unique Visitor)")
    @GetMapping("/recordUV")
    public CommonResult<String> recordUV(@RequestParam String ip){
        homeService.recordUV(ip);
        return CommonResult.success();
    }

    @ApiOperation("统计UV(Unique Visitor)")
    @GetMapping("/getUV")
    public CommonResult getUV(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startDate,
                              @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endDate){
        return CommonResult.success(homeService.calculateUV(startDate,endDate));
    }

    @ApiOperation("统计DAU(Daily Active User)")
    @GetMapping("/getDAU")
    public CommonResult getDAU(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startDate,
                               @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endDate){
        return CommonResult.success(homeService.calculateDAU(startDate,endDate));
    }

    @ApiOperation("文章量")
    @GetMapping("getArticleCount")
    public CommonResult getArticleCount(@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startDate,
                                        @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endDate){
        return CommonResult.success(homeService.getArticleCount(startDate,endDate));
    }

}
